package com.cskaoyan.controller;

import com.cskaoyan.bean.PO.MarketAd;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminAdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 广告管理模块
 *
 * @author wenkaikai
 * @since 2022/08/12 20:44
 */
@RestController
@RequestMapping("admin/ad")
public class AdminAdController {

    @Autowired
    AdminAdService adminAdService;


    /**
     * 创建广告
     * @param record
     * @return
     */
    @RequiresPermissions("admin:ad:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketAd record) {
        // 插入的同时获得插入得到的结果
        MarketAd marketAd = adminAdService.insertAdminAdAndGetResult(record);
        return BaseRespVo.ok(0, marketAd, "成功");
    }

    /**
     * 查询所有广告列表
     * name 和 content用来模糊查询
     *
     * @param pageInfo
     * @param name     :广告标题
     * @param content  ：广告内容
     */
    @RequiresPermissions("admin:ad:list")
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo pageInfo, String name, String content) {
        CommonData<MarketAd> data = adminAdService.queryAdList(pageInfo, name, content);
        return BaseRespVo.ok(0, data, "成功");
    }

    /**
     * 广告删除
     * 逻辑删除
     */
    @RequiresPermissions("admin:ad:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAd marketAd) {
        // 如果已经是删除了的广告则提示不能重复删除
        if(marketAd.getDeleted()){
            return BaseRespVo.custom(400,null,"已删除广告不能重复删除");
        }
        // 删除广告即把对应的deleted 的列修改为 true
        int code = adminAdService.deleteAdminAd(marketAd);
        if(code == 1){
            return BaseRespVo.ok("成功");
        }
        return BaseRespVo.failUpdate();
    }

    /**
     * 编辑广告内容
     * 如果广告的启用位设置成启用，则deleted 位也修改为false
     */
    @RequiresPermissions("admin:ad:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketAd marketAd) {
        int code = adminAdService.updateAd(marketAd);
        if(code == 1) {
            return BaseRespVo.ok(0, null, "成功");
        }
        return BaseRespVo.failUpdate();
    }
}